package com.dd.web.base.service.impl;

import com.dd.web.base.dao.UmsMenuDao;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsMenu;
import com.dd.web.base.service.UmsMenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 后台资源管理Service实现类
 * Created by macro on 2020/2/2.
 */
@Service
public class UmsMenuServiceImpl implements UmsMenuService {
    @Autowired
    private UmsMenuDao menuDao;

    @Override
    public int create(UmsMenu umsResource) {
        return menuDao.insert(umsResource);
    }

    @Override
    public int update(Long id, UmsMenu umsResource) {
        umsResource.setId(id);
        return menuDao.update(umsResource);
    }


    @Override
    public UmsMenu getItem(Long id) {
        UmsMenu menu = menuDao.selectByPrimaryKey(id);
        if (menu.getParentId() !=0){
            //获取父菜单信息
            menu.setParentMenu(menuDao.selectByPrimaryKey(menu.getParentId()));
        }
        return menu;
    }

    @Override
    public int delete(Long id) {
        return menuDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<UmsMenu> listAll() {
        return menuDao.selectUmsMenuAll();
    }

    @Override
    public CommonPage queryUmsMenuForPage(int pageNum, int pageSize,UmsMenu menu) {
        Page<List<UmsMenu>> pageHelp = PageHelper.startPage(pageNum,pageSize);
        List<UmsMenu> data = this.menuDao.selectUmsMenuForPage(menu);
        CommonPage resultPage = CommonPage.restPage(data);
        return resultPage;
    }

    @Override
    public List<UmsMenu> queryMenuByParentId(Long parentId) {
        List<UmsMenu> menuList = menuDao.selectUmsMenuByParentId(parentId);
        int childCount = 0;
        if (menuList != null){
            //查询是否有子菜单
            for (UmsMenu menu : menuList){
                childCount = this.queryUmsMenuCountByParentId(menu.getId());
                menu.setHasChild(childCount>0);
            }
        }
        return menuList;
    }

    @Override
    public int queryUmsMenuCountByParentId(Long parentId) {
        return this.menuDao.selectUmsMenuCountByParentId(parentId);
    }
}